/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ee.ut.math.esi.service;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ool
 */
public class PostWebPageFetcher {

    private static final String END_OF_INPUT = "\\Z";
    private URL url;
    private String data;

    public PostWebPageFetcher(URL url, String data) {
        this.url = url;
        this.data = data;
    }

    public String getPageContent() throws MalformedURLException {
        try {
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            Scanner scanner = new Scanner(conn.getInputStream());
            scanner.useDelimiter(END_OF_INPUT);
            String result = scanner.next();
            return result;
        } catch (IOException ex) {
            Logger.getLogger(PostWebPageFetcher.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
